﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BluePortal.Framework;
using BluePortal.Medias.Core;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;

namespace BluePortal.Medias.Services.Rest
{
    [DataContract]
    public class VideoPublic
    {
        [DataMember]
        public Guid ID;
        [DataMember]
        public string Title;
    }

    [ServiceContract]    
    public interface IMediaRest
    {
        [OperationContract]
        [WebGet(UriTemplate = "FindById/{id}", ResponseFormat = WebMessageFormat.Xml)]
        Video FindById(string id);

        [OperationContract]
        [WebGet(UriTemplate = "All?count={count}", ResponseFormat = WebMessageFormat.Xml)]
        List<Video> FindAll(string count);

        [OperationContract]
        [WebGet(UriTemplate = "Channel/{cid}?count={count}", ResponseFormat = WebMessageFormat.Xml)]
        List<Video> FindByChannel(string cid, string count);

        [OperationContract]
        [WebGet(UriTemplate = "Tag/{tag}?count={count}", ResponseFormat = WebMessageFormat.Xml)]
        List<VideoPublic> FindByTag(string tag, string count);

        [OperationContract]
        [WebGet(UriTemplate = "Tags?start={start}&count={count}", ResponseFormat = WebMessageFormat.Xml)]
        List<string> FindTags(string start, string count);        

        [OperationContract]
        [WebGet(UriTemplate = "Channels?count={count}", ResponseFormat = WebMessageFormat.Xml)]
        List<Channel> FindChannels(string count);
    }

    public class MediaRest : IMediaRest
    {
        private BCVideo bc = new BCVideo();

        #region IMediaRest Members

        public Video FindById(string id)
        {
            return bc.FindVideoByID(new Guid(id));
        }

        public List<Video> FindAll(string count)
        {
            FindParameters p = null;
            if (count != null) p = new FindParameters() { Count = Int32.Parse(count) };
            return bc.FindAll(p).ToList();
        }

        public List<Video> FindByChannel(string cid, string count)
        {
            FindParameters p = null;
            if (count != null) p = new FindParameters() { Count = Int32.Parse(count) };
            return bc.FindByChannel(new Guid(cid), p).ToList();
        }

        public List<VideoPublic> FindByTag(string tag, string count)
        {
            FindParameters p = null;
            if (count != null) p = new FindParameters() { Count = Int32.Parse(count) };
            var q = bc.FindByTag(tag, p);
            return q.Select(v => new VideoPublic { ID = v.ID, Title = v.Title }).ToList();
        }

        public List<string> FindTags(string start, string count)
        {
            FindParameters p = null;
            if (count != null) p = new FindParameters() { Count = Int32.Parse(count) };
            BCTag bctag = new BCTag();
            IEnumerable<Tag> tags = null;
            if (start == null)
                tags = bctag.FindAll(p);
            else
                tags = bctag.FindStartWith(start);
            List<string> res = new List<string>();
            foreach (Tag t in tags) res.Add(t.TagName);
            return res;
        }

        public List<Channel> FindChannels(string count)
        {
            FindParameters p = null;
            if (count != null) p = new FindParameters() { Count = Int32.Parse(count) };
            BCChannel bcchannel = new BCChannel();
            return bcchannel.FindAll(p).ToList(); 
        }
        #endregion
    }
}
